﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Linq.Expressions;
using System.Web.Security;
using AL.Common;
using AL.DAL;
using AL.DAL.Core;
using AL.DAL.Models;


namespace AL.BLL {

    /// <summary>
    /// HH: 角色 业务逻辑操作实现类
    /// </summary>
    public class RoleService : ServiceBase<Role>, IRoleService {
        private readonly IUnitOfWork unitOfWork;
        private readonly IUserRepository userRepository;
        private readonly IRoleRepository roleRepository;
        //private readonly MembershipProvider _Provider;

        public RoleService(IUnitOfWork unitOfWork, IUserRepository userRepository, IRoleRepository roleRepository) {
            //_Provider = provider ?? Membership.Provider;

            this.unitOfWork = unitOfWork;
            this.userRepository = userRepository;
            this.roleRepository = roleRepository;

            // 注意：一定要注册这个对象，否则分页查询会无效
            _Repository = roleRepository;
        }





        #region 　< Role 角色相关操作 >


        public void CreateRole(Role role) {
            roleRepository.Create(role);
            Save();
        }
        public void UpdateRole(Role role) {
            roleRepository.Update(role);
            Save();
        }
        public void DeleteRole(int roleID) {
            var role = GetRole(roleID);
            roleRepository.Delete(role);
            Save();
        }
        // 提交保存操作
        public void Save() {
            unitOfWork.Commit();
        }


        public Role GetRole(int roleID) {
            var role = roleRepository.GetById(roleID);
            return role;
        }
        public IEnumerable<Role> GetRoles() {
            var roles = roleRepository.GetAll();
            return roles;
        }

        #endregion






        /// <summary>
        /// HH: 分配动作权限
        /// </summary>
        public void AssignActionPermission(int roleID, List<int> permissionIDs) {
            roleRepository.AssignActionPermission(roleID, permissionIDs);
            Save();
        }


        /// <summary>
        /// HH: 清除角色的所有权限
        /// </summary>
        public void ClearActionPermission(int roleID) {
            roleRepository.ClearActionPermission(roleID);
            Save();
        }



    }






    /// <summary>
    /// HH: 用户角色 业务逻辑操作 接口类
    /// </summary>
    public interface IRoleService : IServiceBase<Role> {



        #region 　< Role 角色相关操作 >

        /// <summary>
        /// HH: 创建角色
        /// </summary>
        void CreateRole(Role role);
        /// <summary>
        /// HH: 修改角色
        /// </summary>
        void UpdateRole(Role role);
        /// <summary>
        /// HH: 根据角色ID删除一个角色
        /// </summary>
        void DeleteRole(int roleID);


        /// <summary>
        /// HH: 保存操作
        /// </summary>
        void Save();




        /// <summary>
        /// HH: 根据角色ID获取一个角色对象
        /// </summary>
        Role GetRole(int roleID);
        /// <summary>
        /// HH: 获取所有角色集合
        /// </summary>
        IEnumerable<Role> GetRoles();

        #endregion



        /// <summary>
        /// HH: 分配动作权限
        /// </summary>
        void AssignActionPermission(int roleID, List<int> permissionIDs);

        /// <summary>
        /// HH: 清除角色的所有权限
        /// </summary>
        void ClearActionPermission(int roleID);


    }

}